import 'package:easy_read_comic/controller/me/login_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class LoginView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _LoginView();
}

class _LoginView extends State<LoginView> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder(
      builder: (controller) {
        return Form(
          key: controller.formKey,
          child: Container(
            alignment: Alignment.bottomLeft,
            padding: EdgeInsets.all(30),
            child: Column(
              children: [
                Container(
                  child: TextFormField(
                    controller: controller.emailTextEditingController,
                    decoration: InputDecoration(
                      prefixIcon: Icon(
                        Icons.email,
                      ),
                      hintText: "电子邮件",
                    ),
                    validator: (value) {
                      if (value == null) {
                        return "请输入邮件地址";
                      }
                      if (!value.isEmail) {
                        return "输入的电子邮件不正确";
                      }
                      return null;
                    },
                  ),
                ),
                Container(
                  child: TextFormField(
                    controller: controller.passwordTextEditingController,
                    obscureText: !controller.visibility,
                    decoration: InputDecoration(
                      prefixIcon: Icon(Icons.password),
                      hintText: "密码",
                      suffixIcon: IconButton(
                        onPressed: () => controller.changeVisibility(),
                        icon: Icon(
                          !controller.visibility
                              ? Icons.visibility_off
                              : Icons.visibility,
                        ),
                      ),
                    ),
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return "请输入密码";
                      }
                      return null;
                    },
                  ),
                  margin: EdgeInsets.only(
                    top: 30,
                  ),
                ),
                Container(
                  width: Get.width,
                  child: TextButton(
                    onPressed: () => controller.login(),
                    child: Text(
                      "登录",
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    ),
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(
                          Theme.of(context).primaryColor),
                      padding: MaterialStateProperty.all(
                        EdgeInsets.only(
                          top: 20,
                          bottom: 20,
                        ),
                      ),
                    ),
                  ),
                  margin: EdgeInsets.only(
                    top: 50,
                  ),
                ),
              ],
            ),
          ),
        );
      },
      init: LoginController(),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
